package model

import (
	"common/globals"
	"gorm.io/gorm"
)

type Order struct {
	gorm.Model
	OrderId       string  `gorm:"column:order_id;type:varchar(50);not null;index:idx_orderId,unique" json:"order_id"`
	UserId        int     `gorm:"column:user_id;type:int;not null" json:"user_id"`
	MealProductId int     `gorm:"column:meal_product_id;type:int;not null" json:"meal_product_id"`
	Num           int     `gorm:"column:num;type:int;not null" json:"num"`
	PayType       int     `gorm:"column:pay_type;type:int;not null" json:"pay_type"`                       //支付方式
	TotalPrice    float64 `gorm:"column:total_price;type:decimal(10,2);not null" json:"total_price"`       //总价
	DiscountPrice float64 `gorm:"column:discount_price;type:decimal(10,2);not null" json:"discount_price"` //优惠金额
	ActualPrice   float64 `gorm:"column:actual_price;type:decimal(10,2);not null" json:"actual_price"`     //实际金额
	Status        int     `gorm:"column:status;type:int;not null;default:0" json:"status"`                 //0 未付款 1 交易超时关闭或支付后全额退款 2交易支付成功 3交易结束 不可退款
	Comment       string  `gorm:"column:comment;type:text" json:"comment"`                                 //备注
}

func (o *Order) TableName() string {
	return "order"
}
func (o *Order) GetOrderInfoByOrderId(orderId string) error {
	return globals.Db.Where("order_id = ?", orderId).Limit(1).Find(&o).Error
}
func (o *Order) Create() error {
	return globals.Db.Create(&o).Error
}
func (o *Order) UpdateStatusByOrderId(orderId string, status int) error {
	return globals.Db.Debug().Model(&Order{}).Where("order_id = ?", orderId).Update("status", status).Error
}
func (o *Order) GetAllOrderByUserid(userId int) ([]Order, error) {
	var orders []Order
	err := globals.Db.Where("user_id = ?", userId).Order("status asc").Find(&orders).Error
	return orders, err
}
